package learning.spring.contacts.service.jpa;

import learning.spring.contacts.domain.Contact;
import learning.spring.contacts.service.ContactService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: bibro
 * Date: 04.03.14
 * Time: 20:02
 * To change this template use File | Settings | File Templates.
 */
@Service("contactService")
@Transactional
public class ContactServiceImplSec implements ContactService {

    private final Logger logger = LoggerFactory.getLogger(ContactServiceImplSec.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Iterable<Contact> findAll() {
        TypedQuery<Contact> namedQuery = entityManager.createNamedQuery("Contact.findAll", Contact.class);
        List<Contact> contacts = namedQuery.getResultList();
        return contacts;
    }

    @Override
    public Contact findById(Long id) {
        return entityManager.find(Contact.class, id);
    }

    @Override
    public Contact save(Contact contact) {
        entityManager.persist(contact);
        return contact;
    }

    @Override
    public Contact update(Contact contact) {
        logger.info("UPDATE");
        logger.debug(contact.toString());
        return entityManager.merge(contact);
    }

    @Override
    public Iterable<Contact> findByFirstName(String firstName) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Iterable<Contact> findByLastName(String lastName) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Iterable<Contact> findByFistNameAndLastName(String firstName, String lastName) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
